<?php
/**
 *泰外交认证转翻译
*/
namespace app\admin\controller;
use app\admin\controller\Admin;
use think\facade\Db;
use xhadmin\service\admin\record\CustomerService;
use xhadmin\service\admin\MemberService;
use xhadmin\db\Customer as CustomerDb;
use think\facade\Cache;

class Steptwo extends Admin {


	/*会员管理*/
	function index(){
        if (!$this->request->isAjax()){
			return $this->display('index');
		}else{
            $limit  = $this->request->post('limit', 0, 'intval');
			$offset = $this->request->post('offset', 0, 'intval');
			$page   = floor($offset / $limit) +1 ;

			$id_card= $this->request->param('id_card', '', 'strip_tags,trim');
			$first_name = $this->request->param('first_name', '', 'strip_tags,trim');
			$birth_year = $this->request->param('birth_year', '', 'strip_tags,trim');
			$last_name = $this->request->param('last_name', '', 'strip_tags,trim');
            $id= $this->request->param('id', '', 'strip_tags,trim');

            if(!empty($id)){
                $where['id'] = ['=',$id];
            }
            if(!empty($id_card)){
                $where['id_card'] = ['like','%'.$id_card.'%'];
            }
            if(!empty($birth_year)){
                $where['birth_day'] = ['like','%'.$birth_year.'%'];
            }
            if(!empty($first_name)){
                $where['first_name|last_name|passport_info'] = ['like','%'.$first_name.'%'];
            }
            if(!empty($last_name)){
                $where['last_name'] = ['like','%'.$last_name.'%'];
            }
            $where['status'] = ['=',2];
			$limit = ($page-1) * $limit.','.$limit;
            //p($where);exit;
			try{
				$res = CustomerService::pageList(formatWhere($where),$limit,'*',$orderby = 'id desc');
				foreach ($res['list'] as $key=>&$val) {
				    $val['work_year_month'] = $val['work_year'].'年'.$val['work_month'].'个月';
                    $val['name'] = $val['first_name'].' '.$val['last_name'];
                }
				$list = $res['list'];
			}catch(\Exception $e){
				exit($e->getMessage());
			}

			$data['rows']  = $list;
			$data['total'] = $res['count'];
			return json($data);
		}
	}

    /*外交部改为转为翻译状态*/
    function status() {
        if ($this->request->isPost()){
            $postField = 'ids';
            $data = $this->request->only(explode(',',$postField),'post');
            try {
                $now = time();
                if(strstr($data['ids'],',')) {
                    $ids = explode(',',$data['ids']);
                    CustomerService::editWhere([['id','in',$ids]],['status'=>3]);
                    foreach ($ids as $key=>$val) {
                        $info = CustomerDb::getInfo($val);
                        $customer_log[] = [
                            'c_id' => $val,
                            'id_card' => $info['id_card'],
                            'status' => 3,
                            'create_by' => session('admin.userid'),
                            'create_time' => $now,
                        ];
                    }
                    Db::name("customer_log")->insertAll($customer_log);
                } else {
                    $id = $data['ids'];
                    $info = CustomerDb::getInfo($id);
                    $customer_log = [
                        'c_id' => $id,
                        'id_card' => $info['id_card'],
                        'status' => 3,
                        'create_by' => session('admin.userid'),
                        'create_time' => $now,
                    ];
                    Db::name("customer_log")->insert($customer_log);
                    CustomerService::update(['id'=>$id,'status'=>3]);
                }
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            return json(['status'=>'00','msg'=>'修改成功']);
        }
    }


	/*修改*/
	function update(){

        $idx =  $this->request->post('ids', '', 'strip_tags');
        if(!$idx) $this->error('参数错误');
        try{
            $data['status'] = 3;
            if(strpos($idx)) {
                $where['id'] = explode(',',$idx);
            } else {
                $where['id'] = $idx;
            }
            CustomerService::editWhere($where,$data);
        }catch(\Exception $e){
            $this->error($e->getMessage());
        }
        return json(['status'=>'00','msg'=>'操作成功']);
	}

    /*修改*/
    function updateFee(){
        $postField = 'id,id_card_certification_fee,pass_port_certification_fee';
        $data = $this->request->only(explode(',',$postField),'post');
        try {
            CustomerService::update($data);
            if(!empty($data['pass_port_certification_fee'])) {
                Db::name("turnover")->where(['related_index'=>$data['id'],'subject'=>40])->update(['amount'=>$data['pass_port_certification_fee']]);
                Db::name("profit_loss_log")->where(['related_index'=>$data['id'],'subject'=>40])->update(['outcome_amount'=>$data['pass_port_certification_fee']]);
            }
            if(!empty($data['id_card_certification_fee'])) {
                Db::name("turnover")->where(['related_index'=>$data['id'],'subject'=>30])->update(['amount'=>$data['id_card_certification_fee']]);
                Db::name("profit_loss_log")->where(['related_index'=>$data['id'],'subject'=>30])->update(['outcome_amount'=>$data['id_card_certification_fee']]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        return json(['status'=>'00','msg'=>'修改成功']);

    }

	/*删除*/
	function delete(){
		$idx =  $this->request->post('ids', '', 'strip_tags');
		if(!$idx) $this->error('参数错误');
		try{
			$where['id'] = explode(',',$idx);
            CustomerService::delete($where);
		}catch(\Exception $e){
			$this->error($e->getMessage());
		}
		return json(['status'=>'00','msg'=>'操作成功']);
	}

	/*查看数据*/
	function view(){
		$id = $this->request->get('id','','intval');
		if(!$id) $this->error('参数错误');
		try{
			$this->view->assign('info',checkData(CustomerDb::getInfo($id)));
			return $this->display('record/customer/view');
		} catch (\Exception $e){
			$this->error($e->getMessage());
		}
	}



    function getThumbPic(){
        $detail = $this->request->post('detail','','strval');
        preg_match_all('/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/',$detail,$allImg);
        return json(['status'=>'00','imgurl'=>$allImg[1][0]]);
    }



}

